﻿using FileData.Currency;
using FileData.Utils.Currency;
using FileData.Utils.Currency.FileUtils;
using System;
using System.Collections.Generic;
using static FileData.DataUtils.CurrentlyUtils;

namespace FileData.DataUtils
{
    internal class ShortData
    {
        /// <summary>
        ///缩短整个文件夹下所有TXT和LST文件
        /// </summary>
        /// <param name="Floder"></param>
        public void ShortFloder(String Floder) {
            if (Floder == "")
                return;
            List<string> AllFile = new FloderNameHelper().GetFullFileNameList(Floder);
            foreach (string Line in AllFile) {
                string FileExt = new FileNameHelper().GetFileType(Line);
                if (FileExt != ".lst" && FileExt !=".txt") {
                    Console.WriteLine("{0}文件类型不符合，不缩短。（仅缩短.lst和.txt文件）",Line);
                    continue;
                }
                ShortSingle(Line,false);
            }
            new MessageHelper().OpenFloderAlert(Floder);

        }
        
        /// <summary>
     /// 缩短单个文件
     /// </summary>
     /// <param name="FilePath"></param>
        public void ShortSingle(string FilePath,bool AlertFlage=true)
        {
            string ShortInfoFile = new CurrentlyUtils().GetRuleFile(FilePath);
            if (ShortInfoFile == "")
                return;
            List<ShortDataInfo> ReplaceTextList = new CurrentlyUtils(). ReadDataInfo(ShortInfoFile);
            UpdateFile(FilePath, ReplaceTextList);

            if(AlertFlage)
                new MessageHelper().OpenFloderAlert(new FloderNameHelper().GetFloderPath(FilePath));
        }



        /// <summary>
        /// 更新文件，以_Update结尾
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="replaceTextList"></param>
        private void UpdateFile(string filePath, List<ShortDataInfo> ReplaceTextList)
        {
            List<string> TempList = FileHelper.ReadAllLine(filePath);

            foreach (ShortDataInfo SinleData in ReplaceTextList) {
                
                TempList =new CurrentlyUtils(). RepListHelper(TempList, SinleData.ReplaceForm,SinleData.ReplaceTo);
            }

            FileHelper.WriteUTF8List(new FileNameHelper().CreateFileNameAddTip(filePath,"_Update"),TempList);
        }
     

        
    }
}
 
 